/*
 * 英才汇硕信息科技有限公司 拥有本软件版权 2021 并保留所有权利。
 * Copyright 2021, YCHS Information&Science Techology Co.,Ltd,
 * All right reserved.
 */
package com.ychs.lawyerback.controller;


import com.ychs.lawyerback.annotation.RequiredPermission;
import com.ychs.lawyerback.common.R;
import com.ychs.lawyerback.dto.LawyerOptDTO;
import com.ychs.lawyerback.dto.LawyerQueryDTO;
import com.ychs.lawyerback.pojo.LawyerCertificateEntity;
import com.ychs.lawyerback.pojo.LawyerEntity;
import com.ychs.lawyerback.service.DocumentService;
import com.ychs.lawyerback.service.LawyerService;
import com.ychs.lawyerback.vo.LawyerQueryVo;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author mayujing
 * @version 1.0
 */
@RestController
@RequestMapping("/lawyer")
public class lawyerController {


    @Autowired
    private LawyerService lawyerService;

    @Autowired
    private DocumentService documentService;


    /**
     * 分页查询律师信息
     * @param lawyerQueryDTO
     * @return
     */
    @GetMapping("/list")
//    @RequiredPermission("system:lawyer:query")
    public R<?> getLawyers(LawyerQueryDTO lawyerQueryDTO) {
        Map<String,Object> map = lawyerService.getLawyersByCondition(lawyerQueryDTO);
        return R.success(map);
    }


    /**
     * 查看律师详情（含所有文件）
     */
    @GetMapping("/{lawyerId}")
//    @RequiredPermission("system:lawyer:query")
    public R<?> getLawyerDetail(@PathVariable Long lawyerId) {
        LawyerQueryVo lawyerVo = lawyerService.getLawyersByCondition(lawyerId);
        return R.success(lawyerVo);
    }

    @GetMapping("/byUserId")
    public R<?> getLawyerByUserId(@RequestParam Long userId) {
        // 调用Service层新方法
        LawyerQueryVo  lawyer = lawyerService.getByUserId(userId);
        return R.success(lawyer); // 直接返回单个律师对象
    }


    @PostMapping(value = "/addLawyer",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
//    @RequiredPermission("system:lawyer:add")
    public R<?> addLawyer(@Valid @ModelAttribute LawyerOptDTO lawyerOptDTO,
                          @RequestPart(value = "photo",required = false)MultipartFile photo,
                          @RequestPart(value = "transferRecords",required = false) MultipartFile transferRecords,
                          @RequestPart(value = "fileTransferRecords",required = false)MultipartFile fileTransferRecords,
                          @RequestPart(value = "certificateFiles",required = false)List<MultipartFile> certificateFiles,
                          @RequestParam(value = "certificateNames",required = false)List<String> certificateNames){
        try {
            //绑定文件到 DTO
            lawyerOptDTO.setPhotoFile(photo);
            lawyerOptDTO.setTransferRecordsFile(transferRecords);
            lawyerOptDTO.setFileTransferRecordsFile(fileTransferRecords);
            lawyerOptDTO.setCertificateFiles(certificateFiles);
            lawyerOptDTO.setCertificateNames(certificateNames);

            Long userId = lawyerService.addLawyer(lawyerOptDTO);
            return R.success(Map.of("userId",userId));
        } catch (Exception e) {
            e.printStackTrace(); // 临时快速打印，生产环境建议用logger
            return R.fail();
        }

    }



//    @PostMapping("/addLawyer")
//    @RequiredPermission("system:lawyer:add")
//    public R<?> addLawyer(@Valid @RequestBody LawyerOptDTO lawyerOptDTO) {
//        int res = lawyerService.addLawyer(lawyerOptDTO);
//        return res > 0 ? R.success() : R.fail();
//    }

    @PutMapping(value = "/editLawyer",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
//    @RequiredPermission("system:lawyer:edit")
    public R<?> editLawyer(@Valid @ModelAttribute LawyerOptDTO lawyerOptDTO,
                           @RequestPart(value = "photo",required = false)MultipartFile photo,
                           @RequestPart(value = "transferRecords",required = false) MultipartFile transferRecords,
                           @RequestPart(value = "fileTransferRecords",required = false)MultipartFile fileTransferRecords,
                           @RequestParam(value = "deleteCertificateIds", required = false) List<Long> deleteCertificateIds,
                           @RequestPart(value = "certificateFiles",required = false)List<MultipartFile> certificateFiles,
                           @RequestParam(value = "certificateNames",required = false)List<String> certificateNames) throws IOException {
        try {
            //绑定文件到 DTO
            lawyerOptDTO.setPhotoFile(photo);
            lawyerOptDTO.setTransferRecordsFile(transferRecords);
            lawyerOptDTO.setFileTransferRecordsFile(fileTransferRecords);
            lawyerOptDTO.setDeleteCertificateIds(deleteCertificateIds);
            lawyerOptDTO.setCertificateFiles(certificateFiles);
            lawyerOptDTO.setCertificateNames(certificateNames);

        }catch (Exception e) {
            e.printStackTrace(); // 临时快速打印，生产环境建议用logger
            return R.fail();
        }
        int res = lawyerService.editLawyer(lawyerOptDTO);
        System.out.println("测试：");
        if (res > 0) {
            // 可以返回更新后的律师ID或完整信息
//            return R.success(lawyerOptDTO.getLawyerId());
            // 或者查询最新数据返回
             LawyerQueryVo updatedVo = lawyerService.getLawyersByCondition(lawyerOptDTO.getLawyerId());
            System.out.println("updateVo:"+updatedVo);
             return R.success(updatedVo);
        } else {
            return R.fail();
        }

    }


    @DeleteMapping("/deleteLawyer")
// @RequiredPermission("system:lawyer:delete")
    public R<?> deleteLawyer(@RequestBody List<Long> ids) {
        int res = lawyerService.deleteLawyer(ids);
        return res > 0 ? R.success() : R.fail();
    }

}
